<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="当事人名称" prop="partyName">
        <el-input
          v-model="queryParams.partyName"
          placeholder="请输入当事人名称"
          clearable
          :style="{ width: '220px' }"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="登记人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入登记人"
          clearable
          :style="{ width: '220px' }"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="登记日期" style="width: 308px">
        <el-date-picker
          v-model="daterangeCreateTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :style="{ width: '220px' }"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['partyinfo:partyinfo:add']">
          新增
        </el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['partyinfo:partyinfo:edit']">
          修改
        </el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['partyinfo:partyinfo:remove']">
          删除
        </el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['partyinfo:partyinfo:export']">
          导出
        </el-button>
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" :columns="columns" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="partyinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <template v-for="column in newColumns" :key="column.key">
        <el-table-column
          :label="column.label"
          :prop="column.key"
          :min-width="column.minWidth || ''"
          :width="column.width || ''">
          <template #default="scope" v-if="column.key === 'address'">
            {{
              settings.cityName +
              settings.areaName +
              street_of_area.find(item => item.value === scope.row.streetOfCompanyAddress)?.label +
              scope.row.address
            }}
          </template>
        </el-table-column>
      </template>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        minWidth="150px"
        fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['partyinfo:partyinfo:edit']">
            修改
          </el-button>
          <el-button
            link
            type="danger"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['partyinfo:partyinfo:remove']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改当事人信息对话框 -->
    <detail v-if="open" v-model:value="open" :title="title" :id="formId" @getList="getList" />
  </div>
</template>

<script setup name="Partyinfo">
import { listPartyinfo, delPartyinfo } from '@/api/partyinfo/index'
import detail from './detail.vue'
import settings from '@/settings.js'

const { proxy } = getCurrentInstance()
const { street_of_area } = proxy.useDict('street_of_area')

const partyinfoList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const formId = ref('')
const title = ref('')
const daterangeCreateTime = ref([])

const columns = ref([
  { key: 'partyName', label: '当事人名称', visible: true, minWidth: '180' },
  { key: 'legalRepresentative', label: '法定代表人', visible: true, minWidth: '150' },
  { key: 'creditCode', label: '统一社会信用代码', visible: true, minWidth: '180' },
  { key: 'phone', label: '联系电话', visible: true, minWidth: '150' },
  { key: 'address', label: '地址', visible: true, minWidth: '280' },
  { key: 'createName', label: '登记人', visible: true, minWidth: '120' },
  { key: 'createTime', label: '登记时间', visible: true, width: '160' },
])
const newColumns = computed(() => {
  return columns.value.filter(column => column.visible)
})

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    partyName: null,
    createBy: null,
    createTime: null,
  },
})

const { queryParams } = toRefs(data)

/** 查询当事人信息列表 */
function getList() {
  loading.value = true
  queryParams.value.params = {}
  if (null != daterangeCreateTime.value && '' != daterangeCreateTime.value) {
    queryParams.value.params['beginCreateTime'] = daterangeCreateTime.value[0]
    queryParams.value.params['endCreateTime'] = daterangeCreateTime.value[1]
  }
  listPartyinfo(queryParams.value).then(response => {
    partyinfoList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCreateTime.value = []
  proxy.resetForm('queryRef')
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  formId.value = undefined
  open.value = true
  title.value = '添加当事人信息'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  formId.value = row.id || ids.value
  open.value = true
  title.value = '修改当事人信息'
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal
    .confirm(`是否确定删除 "${row.partyName}" 的数据信息？`)
    .then(function () {
      return delPartyinfo(_ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'partyinfo/partyinfo/export',
    {
      ...queryParams.value,
    },
    `partyinfo_${new Date().getTime()}.xlsx`
  )
}

getList()
</script>
